/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.common.web;

import java.util.List;
import java.util.Set;
import java.util.Iterator;

import com.javaeye.common.dto.Module;
import com.javaeye.common.dto.Role;
import com.javaeye.common.service.IModuleService;
import com.javaeye.common.service.IRoleService;
import com.opensymphony.xwork2.ActionSupport;

public class RoleAction extends ActionSupport {
	
	//private static Log log = LogFactory.getLog(RoleAction.class);
	
	private int roleId;
	
	private IRoleService roleService;
	
	private IModuleService moduleService;
	
	private List<Role> roleList;
	
	private Role role;
	
	private int moduleId;
	
	private List<Module> moduleList;
	
	private List<Module> rootModules;
	
	private int parentModuleId;

	/**
	 * 
	 */
	private static final long serialVersionUID = -7024923899191853934L;
	
	public RoleAction() {
		parentModuleId = -1;
	}
	
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}

	public void setModuleService(IModuleService moduleService) {
		this.moduleService = moduleService;
	}

	public String queryRoleData() throws Exception {
		role = roleService.getRole(roleId);
		return SUCCESS;
	}
	
	public String queryRoleList() throws Exception {
		roleList = roleService.getRoles();
		return SUCCESS;
	}
	
	public String queryModifyRoleList() throws Exception {
		roleList = roleService.getRoles();
		for (Iterator<Role> i = roleList.iterator(); i.hasNext();) { 
			Role element = i.next(); 
		    if (element.getId() == 1) { 
		        i.remove(); 
		    } 
		}
		return SUCCESS;
	}
	
	public String saveRole() throws Exception {
		if (roleId > 0) {
			Role oldRole = roleService.getRole(roleId);
			oldRole.setName(role.getName());
			oldRole.setDescription(role.getDescription());
			roleService.saveRole(oldRole);
		} else {
			roleService.saveRole(role);
		}
		return SUCCESS;
	}
	
	public String queryForRoleModuleConfig() {
		roleList = roleService.getRoles();
		rootModules = moduleService.getModules(0);
		moduleList = moduleService.getModules(parentModuleId);
		return SUCCESS;
	}
	
	public String deleteRole() throws Exception {
		roleService.removeRole(roleId);
		return SUCCESS;
	}
	
	public String changePermissions() throws Exception {
		role = roleService.getRole(roleId);
		Set<Module> modules = role.getModules();
		Module dbModule = findModule(modules, moduleId);
		if (dbModule != null) {
			modules.remove(dbModule);
		} else {
			Module module = moduleService.getModule(moduleId);
			modules.add(module);
		}
		roleService.saveRole(role);
		return SUCCESS;
	}
	
	private Module findModule(Set<Module> modules, int id) {
		for (Module m : modules) {
			if (m.getId() == id) {
				return m;
			}
		}
		return null;
	}

	public int getRoleId() {
		return roleId;
	}

	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}

	public List<Role> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public void setModuleList(List<Module> moduleList) {
		this.moduleList = moduleList;
	}

	public List<Module> getModuleList() {
		return moduleList;
	}

	public int getModuleId() {
		return moduleId;
	}

	public void setModuleId(int moduleId) {
		this.moduleId = moduleId;
	}

	public void setRootModules(List<Module> rootModules) {
		this.rootModules = rootModules;
	}

	public List<Module> getRootModules() {
		return rootModules;
	}

	public void setParentModuleId(int parentModuleId) {
		this.parentModuleId = parentModuleId;
	}

	public int getParentModuleId() {
		return parentModuleId;
	}
	
}
